package com.gargi.crimepatrol.main;

import javax.microedition.io.ConnectionNotFoundException;
import javax.microedition.midlet.MIDlet;

import com.gargi.crimpatrol.utils.Constant;
import com.gargi.crimpatrol.utils.DialogManager;
import com.nokia.mid.ui.orientation.Orientation;
import com.nokia.mid.ui.orientation.OrientationListener;
import com.sun.lwuit.Display;

/**
 * This is the main MIDlet of the template. It handles MIDlet life cycle
 * management (what to do when MIDlet starts, pauses or closes) as well as view
 * stack management via the ViewManager interface. There should be no reason to
 * bloat this class with massive amounts of code; it is better to try to clearly
 * separate responsibilities to own classes.
 */
public class CrimePatrolMidlet
    extends MIDlet
    implements OrientationListener {
	
	private VideoListView m_videolistview;
	private DialogManager m_dialogmanager;
    
    // Life cycle management

    /**
     * What to do when MIDlet is started
     * @see javax.microedition.midlet.MIDlet#startApp()
     */
    public void startApp() {
    	
    	Display.init(this);
    	Display.getInstance().setForceFullScreen(true);
        Orientation.addOrientationListener(this);

    	initViews();
    	showVideoListView();        
    }

    /**
     * What to do when MIDlet is paused. This can happen e.g. when MIDlet is on
     * the foreground and an incoming phone call is received.
     * @see javax.microedition.midlet.MIDlet#pauseApp()
     */
    public void pauseApp() {
    }

    /**
     * What to do when MIDlet is destroyed
     * @param unconditional If false, MIDlet can ask to not to be destroyed by
     *        throwing MIDletStateChangeException. If true, MIDlet will be
     *        destroyed regardless of how this function terminates.
     * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
     */
    public void destroyApp(boolean unconditional) {
    }

    /**
     * Changes display orientation between landscape and portrait.
     * Note: ORIENTATION_LANDSCAPE_180 and ORIENTATION_PORTRAIT_180 are not
     * supported.
     * @param newDisplayOrientation Tells the new device orientation.
     * @see com.nokia.mid.ui.orientation.OrientationListener#displayOrientationChanged(int)
     */
    public void displayOrientationChanged(int newDisplayOrientation) {
        if (newDisplayOrientation == Orientation.ORIENTATION_LANDSCAPE
            || newDisplayOrientation == Orientation.ORIENTATION_PORTRAIT)
        {
            Orientation.setAppOrientation(newDisplayOrientation);
        }
    }
    
    private void initViews() {
    	m_videolistview = new VideoListView(this);
    }
    
    private void showVideoListView() {
    	m_videolistview.show();
    }
    
    public void playVideo(String url) {
    	try {
			platformRequest(url);
		} catch (ConnectionNotFoundException e) {
			e.printStackTrace();
		}
    }
    
    public void showAboutDialog() {
    	m_dialogmanager.showAboutDialog();
    }
    
    public String getAppName() {
    	return getAppProperty(Constant.MIDLET_NAME);
    }
    
    public String getAppVersion() {
    	return getAppProperty(Constant.MIDLET_VERSION);
    }
    
    public String getAppVendor() {
    	return getAppProperty(Constant.MIDLET_VENDOR);
    }

}
